package com.itheima.listen;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @作者 itcast
 * @创建日期 2020/11/4 15:09
 **/
@Component
public class ListenMsg {
    // 幂等性
    @RabbitListener(queues = "hello")
    public void handleMsg(@Payload String str,Message message){
        String messageId = message.getMessageProperties().getMessageId();
        // 支付宝  微信
        System.out.println("接收到的消息==>"+str);
        System.out.println(1/0);
        System.out.println("接收到的消息==>"+str);
    }
    /**
     *      * 监听: topic_queue 队列
     *      * 交换器:  spring_topic_ex  主题交换器
     *      * 绑定:  route_key:  #.key.#
     * @param map
     */
    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(value = "topic_queue",durable = "true"),
                            exchange = @Exchange(value = "spring_topic_ex",type = "topic"),
                            key = "#.key.#"
                    )
            }
    )
    // 如果需要手动确认消息,消息的参数列表需要注入 Channel Message
    public void handleMsg2(@Payload Map map, Channel channel, Message message){
        System.out.println("接收到的消息==>"+map);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(deliveryTag,true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
